package com.mt.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mt.enums.Status;
import com.mt.exception.GlobalException;
import com.mt.enums.SeatStatus;
import com.mt.mapper.CinemaScheduleMapper;
import com.mt.mapper.CinemaSeatMapper;
import com.mt.pojo.CinemaSchedule;
import com.mt.pojo.CinemaSeat;
import com.mt.service.CinemaSeatService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Wang
 * @since 2020-12-11
 */
@Service
public class CinemaSeatServiceImpl extends ServiceImpl<CinemaSeatMapper, CinemaSeat> implements CinemaSeatService {

    @Resource
    CinemaSeatMapper seatMapper;

    @Resource
    CinemaScheduleMapper scheduleMapper;

    @Override
    public List<CinemaSeat> listSeatByHallId(int currentPage, int pageSize, int hallId) {
        IPage<CinemaSeat> page = new Page<>(currentPage,pageSize);
        List<CinemaSeat> cinemaSeats = seatMapper.pageSeatList(page, hallId);
        return cinemaSeats;
    }

    public  List<CinemaSeat> listShowSeats(int scheduleId){
        //todo 可能修改: 前端可直接传hallId / schedule对象?
        CinemaSchedule schedule = scheduleMapper.selectOne(Wrappers.<CinemaSchedule>lambdaQuery()
                .eq(CinemaSchedule::getId, scheduleId));
        if (schedule==null){
            throw new GlobalException(Status.NOT_EXIST_SCHDULE);
        }

        List<CinemaSeat> availableSeats = seatMapper.selectList(Wrappers.<CinemaSeat>lambdaQuery()
                .eq(CinemaSeat::getHallId, schedule.getHallId())
                .eq(CinemaSeat::getStatus, SeatStatus.ON.getCode()));
        //todo 通过订单接口获取已购买的订单  getSoldTicket()

        List<Integer> unavailableSeats = CollUtil.newArrayList();
        for (CinemaSeat seat : availableSeats) {
            if (unavailableSeats.contains(seat.getId())) {
                seat.setAvailable(false);
            } else {
                seat.setAvailable(true);
            }
        }
        return availableSeats;
    }


    @Override
    public boolean defaultSeat2Hall(int hallId, int maxRow, int maxColumn) {
        ArrayList<CinemaSeat> list = CollUtil.newArrayList();
        for (int row = 1; row <= maxRow; row++) {
            for (int col = 1; col <= maxColumn; col++) {
                list.add(new CinemaSeat(hallId, row, col));
            }
        }
        return addSeat2Hall(list, hallId);
    }

    @Override
    public boolean addSeat2Hall(List<CinemaSeat> seatList, int hallId) {
        if (seatList.isEmpty()) {
            throw new GlobalException(Status.SEAT_LIST_EMPTY);
        }
        boolean insert = seatMapper.batchInsertSeats(seatList,hallId);
        if (!insert) {
            throw new GlobalException(Status.ERROR);
        }
        return true;
    }

    @Override
    public boolean changeStatusMultipleSeats(List<Integer> seatIdList, int status) {
        if (seatIdList.isEmpty()) {
            throw new GlobalException(Status.SEAT_LIST_EMPTY);
        }
        seatMapper.changeStatusMultipleSeats(seatIdList, status);
        return true;
    }
    @Override
    public boolean deleteSeat(Integer seatId) {
        if (seatMapper.deleteById(seatId) > 0) {
            return true;
        } else {
            throw new GlobalException(Status.ERROR);
        }

    }

    @Override
    public boolean deleteAllHallSeat(Integer hallId) {
        int delete = seatMapper.delete(Wrappers.<CinemaSeat>lambdaQuery().eq(CinemaSeat::getHallId, hallId));
        return delete >0;
    }

    @Override
    public boolean batchDeleteSeats(List<CinemaSeat> seatIdList) {
        if (seatMapper.deleteBatchIds(seatIdList) > 0) {
            return true;
        } else {
            throw new GlobalException(Status.ERROR);
        }

    }
}
